<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-03-15
 * Time: 16:34
 */
namespace app\home\controller;

use think\Controller;
use think\Db;


class Crontab extends Controller
{
    public function get_goods(){
        $cate_id = input('param.cate_id','');//分类id
        if (!empty($cate_id)){
            $url = "http://v2.api.haodanku.com/itemlist/apikey/shen/nav/3/cid/".$cate_id."/back/1000/min_id/1";
            self::goods($cate_id,$url);
        }else{
            exit();
        }
    }

    /**
     * 未使用
     * @param $cate_id
     * @param $url
     */
    public function goodsnew($cate_id,$url){
        $data = file_get_contents($url);
        $content_ori=strip_tags($data);
        $content_arr=json_decode($content_ori,true);
        $info = [];
        if (!empty($content_arr['data'])){
            foreach ($content_arr['data'] as $k=>$v){
                if ($v['activityid']){
                    $info['couponurl'] = "https://uland.taobao.com/coupon/edetail?activityId=".$v['activityid']."&itemId=".$v['itemid']."&src=cd_cdll";
                }
                $info[]['fqcat'] = $v['fqcat'];//分类
                $info['itemid'] = $v['itemid'];//淘宝商品id
                $info['itemtitle'] = $v['itemtitle'];//宝贝标题
                $info['itemshorttitle'] = $v['itemshorttitle'];//宝贝短标题
                $info['itemdesc'] = $v['itemdesc'];//宝贝推荐语
                $info['itemprice'] = $v['itemprice'];//在售价
                $info['itemsale'] = $v['itemsale'];//宝贝月销量
                $info['itemsale2'] = $v['itemsale2'];//宝贝近2小时跑单
                $info['todaysale'] = $v['todaysale'];//当天销量
                $info['itempic'] = $v['itempic'];//宝贝主图原始图像
                $info['itempic_copy'] = $v['itempic_copy'];//推广长图
                $info['itemendprice'] = $v['itemendprice'];//宝贝券后价
                $info['shoptype'] = $v['shoptype'];//店铺类型
                $info['couponmoney'] = $v['couponmoney'];//优惠券金额
                $info['is_brand'] = $v['is_brand'];//是否品牌产品
                $info['is_live'] = $v['is_live'];//是否为直播
                $info['guide_article'] = $v['guide_article'];//推广导购文案
                $info['videoid'] = $v['videoid'];//商品视频id
                $info['activity_type'] = $v['activity_type'];//活动类型
                $info['sellernick'] = $v['sellernick'];//店铺掌柜名
                $info['userid'] = $v['userid'];//店主的userid
                $info['tktype'] = $v['tktype'];//佣金计划
                $info['tkrates'] = $v['tkrates'];//佣金比例
                $info['tkmoney'] = $v['tkmoney'];//预计可得
                $info['tkurl'] = $v['tkurl'];//定向计划链接
                $info['couponreceive2'] = $v['couponreceive2'];//当天优惠券领取数量
                $info['couponnum'] = $v['couponnum'];//优惠券总数量
                $info['couponexplain'] = $v['couponexplain'];//优惠券使用条件
                $info['couponstarttime'] = $v['couponstarttime'];//优惠券开始时间
                $info['couponendtime'] = $v['couponendtime'];//优惠券结束时间
                $info['start_time'] = $v['start_time'];//活动开始时间
                $info['end_time'] = $v['end_time'];//活动结束时间
                $info['is_explosion'] = $v['is_explosion'];//是否为爆款
                $info['activityid'] = $v['activityid'];//优惠券id
                $info['coupon_condition'] = $v['coupon_condition'];//优惠券使用条件
                $info['product_id'] = $v['product_id'];//好单库商品id
            }
            Db::table('yf_items')->insertAll($info,true);
            sleep(2);
            if ($k == 999){
                $url = "http://v2.api.haodanku.com/itemlist/apikey/shen/nav/3/cid/".$cate_id."/back/1000/min_id/".$content_arr['min_id'];
                self::goods($cate_id,$url);
            }else{
                exit();
            }
        }else{
            exit();
        }
    }
    /**
     *
     * @param $cate_id
     * @param $url
     */
    public function goods($cate_id,$url){
        $data = file_get_contents($url);
        $content_ori=strip_tags($data);
        $content_arr=json_decode($content_ori,true);
        foreach ($content_arr['data'] as $k=>$v){
            if ($v['activityid'])
                $content_arr['data'][$k]['couponurl'] = "https://uland.taobao.com/coupon/edetail?activityId=".$v['activityid']."&itemId=".$v['itemid']."&src=cd_cdll";
            unset($content_arr['data'][$k]['me'],$content_arr['data'][$k]['original_article'],$content_arr['data'][$k]['original_img'],$content_arr['data'][$k]['seller_name'],$content_arr['data'][$k]['general_index'],$content_arr['data'][$k]['report_status'],$content_arr['data'][$k]['cuntao'],$content_arr['data'][$k]['online_users']);
            unset($content_arr['data'][$k]['planlink'],$content_arr['data'][$k]['discount'],$content_arr['data'][$k]['isquality'],$content_arr['data'][$k]['seller_id'],$content_arr['data'][$k]['couponsurplus'],$content_arr['data'][$k]['couponreceive']);
            unset($content_arr['data'][$k]['todaycouponreceive'],$content_arr['data'][$k]['starttime'],$content_arr['data'][$k]['taobao_image']);
        }
        Db::table('yf_items')->insertAll($content_arr['data'],true);
        sleep(2);
        if ($k == 999){
            $url = "http://v2.api.haodanku.com/itemlist/apikey/shen/nav/3/cid/".$cate_id."/back/1000/min_id/".$content_arr['min_id'];
            $this->goods($cate_id,$url);
        }else{
            exit();
        }
    }


    /**
     * 定时删除过期的好货
     */
    public function del_good(){
        Db::startTrans();
        try{
            $flag = Db::table('yf_good')->where(['end_time'=>['lt',time()]])->delete();
            if ($flag){
                echo 'success'.date("Y-m-d H:i:s", time());
                Db::commit();
            }else{
                throw new Exception('失败');
            }
        }catch (Exception $e){
            Db::rollback();
            echo 'fail'.date("Y-m-d H:i:s", time());
        }
    }

    /**
     * 获取好单库数据
     */
    public function getgoods($url='http://v2.api.haodanku.com/itemlist/apikey/shen/nav/3/cid/0/back/1000/min_id/1'){
        unset($data);
        $data = file_get_contents($url);
        $data=json_decode($data,true);
        unset($info);
        if (!empty($data['data'])) {
            $num = count($data['data']);
            Db::startTrans();
            foreach ($data['data'] as $k => $v) {
                if ($v['activityid']) {
                    $info[$k]['couponurl'] = "https://uland.taobao.com/coupon/edetail?activityId=" . $v['activityid'] . "&itemId=" . $v['itemid'] . "&src=cd_cdll";
                }else{
                    $info[$k]['couponurl'] = '';
                }
                $info[$k]['fqcat'] = $v['fqcat'];//分类
                $info[$k]['itemid'] = $v['itemid'];//淘宝商品id
                $info[$k]['itemtitle'] = $v['itemtitle'];//宝贝标题
                $info[$k]['itemshorttitle'] = $v['itemshorttitle'];//宝贝短标题
                $info[$k]['itemdesc'] = $v['itemdesc'];//宝贝推荐语
                $info[$k]['itemprice'] = $v['itemprice'];//在售价
                $info[$k]['itemsale'] = $v['itemsale'];//宝贝月销量
                $info[$k]['itempic'] = $v['itempic'];//宝贝主图原始图像
                $info[$k]['itemendprice'] = $v['itemendprice'];//宝贝券后价
                $info[$k]['shoptype'] = $v['shoptype'];//店铺类型
                $info[$k]['couponmoney'] = $v['couponmoney'];//优惠券金额
                $info[$k]['is_brand'] = $v['is_brand'];//是否品牌产品
                $info[$k]['is_live'] = $v['is_live'];//是否为直播
                $info[$k]['guide_article'] = $v['guide_article'];//推广导购文案
                $info[$k]['videoid'] = $v['videoid'];//商品视频id
                $info[$k]['activity_type'] = $v['activity_type'];//活动类型
                $info[$k]['sellernick'] = $v['sellernick'];//店铺掌柜名
                $info[$k]['userid'] = $v['userid'];//店主的userid
                $info[$k]['tktype'] = $v['tktype'];//佣金计划
                $info[$k]['tkrates'] = $v['tkrates'];//佣金比例
                $info[$k]['tkmoney'] = $v['tkmoney'];//预计可得
                $info[$k]['tkurl'] = $v['tkurl'];//定向计划链接
                $info[$k]['couponreceive2'] = $v['couponreceive2'];//当天优惠券领取数量
                $info[$k]['couponnum'] = $v['couponnum'];//优惠券总数量
                $info[$k]['couponexplain'] = $v['couponexplain'];//优惠券使用条件
                $info[$k]['couponstarttime'] = $v['couponstarttime'];//优惠券开始时间
                $info[$k]['couponendtime'] = $v['couponendtime'];//优惠券结束时间
                $info[$k]['start_time'] = $v['start_time'];//活动开始时间
                $info[$k]['end_time'] = $v['end_time'];//活动结束时间
                $info[$k]['is_explosion'] = $v['is_explosion'];//是否为爆款
                $info[$k]['activityid'] = $v['activityid'];//优惠券id
                $info[$k]['coupon_condition'] = $v['coupon_condition'];//优惠券使用条件
                $info[$k]['product_id'] = $v['product_id'];//好单库商品id
            }
            Db::table('yf_items')->insertAll($info,true);
            Db::commit();
            $id = $data['min_id'];
            sleep(2);
            unset($data);
            if ($num == 1000){
                self::getgoods('http://v2.api.haodanku.com/itemlist/apikey/shen/nav/3/cid/0/back/1000/min_id/'.$id);
            }else{
                exit();
            }
        }else{
            exit();
        }
    }


}